package com.fenghongzhang.day006;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.widget.Toast;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private ViewPager vp;
    private TabLayout tab;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tab = (TabLayout) findViewById(R.id.tab);

        //标题的数据源
        List<String> titleList = new ArrayList<>();
        titleList.add("音乐");
        titleList.add("游戏");
        titleList.add("网络");



        vp = (ViewPager) findViewById(R.id.vp);

//        //1,数据源
//        List<String> list = new ArrayList<>();
//        list.add("第一页");
//        list.add("第二页");
//        list.add("第三页");
//        //2,适配器
//        VpAdapter vpAdapter = new VpAdapter(this,list);
//        //3设置适配
//        vp.setAdapter(vpAdapter);

        //fragment
        //数据源
        final List<Fragment> list = new ArrayList<>();
        list.add(new OneFragment());
        list.add(new TwoFragment());
        list.add(new ThreeFragment());


        //适配器
        FgAdapter fgAdapter = new FgAdapter(getSupportFragmentManager(), list,titleList);
        vp.setAdapter(fgAdapter);

        vp.setPageTransformer(true,new DepthPageTransformer());

//        final Timer timer = new Timer();
//        timer.schedule(new TimerTask() {
//            int index = 0;
//            @Override
//            public void run() {
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        vp.setCurrentItem(index++);
//                    }
//                });
//                if(index > list.size()){
//                   timer.cancel();
//                }
//            }
//        },0,1000);

        //设置当前的页
//        vp.setCurrentItem(1);


        //4. 绑定
        tab.setupWithViewPager(vp);

        //设置翻页监听器
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Toast.makeText(MainActivity.this, "当前页是"+position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
